
export function isDef(val) {
  return val !== undefined && val !== null
}

export function isPromise(val) {
  return (
    isDef(val) &&
    typeof val.then === 'function' &&
    typeof val.catch === 'function'
  )
}

export function getAndRemoveValue(obj, key) {
  const value = obj[key]
  delete obj[key]
  return value
}

/**
 * 用一个路径去对象中查找
 *
 * @param {*} obj 数据
 * @param {*} path 路径
 */
export function parsePath(obj, path) {
  if (path) {
    const segments = path.split('.')
    for (let i = 0; i < segments.length; i++) {
      if (!obj) return
      obj = obj[segments[i]]
    }
  }

  return obj
}

/**
 * 驼峰转短横线
 * @example
 * ```js
 * var str = 'MyName' || 'myName'
 * console.log(toKebabCase(str)) // my-name
 * ```
 */
export function toKebabCase(val) {
  return val.replace(/[A-Z]/g, (char, index) => {
    char = char.toLowerCase()
    return index === 0 ? char : '-' + char
  })
}
